/*
 * Created on Jul 14, 2005
 *
 */
package com.erclab.internal.xpresso.reports.filters;

import java.util.HashMap;

import com.erclab.internal.xpresso.dataAccess.Query;
import com.erclab.internal.xpresso.reports.Report;
import com.erclab.internal.xpresso.reports.ReportFilter;

/**
 * @author Enrique Rodriguez.
 *
 */
public class MysqlSimpleColumnFilter extends ReportFilter {

	/* (non-Javadoc)
	 * @see com.erclab.internal.xpresso.reports.ReportFilter#applyFilter(java.lang.String, java.lang.String[])
	 */
	public void applyFilterToQuery(Query query, HashMap conditions) {
		String[] values = (String[])conditions.get("Filter" +  this.columnToFilter); 
		String[] conditionsArray = (String[])conditions.get("Filter" +  this.columnToFilter + "Condition");
		if (values == null || conditionsArray == null)
			return;
		
		String conditionType =	conditionsArray[0];
		
		if (values != null && values[0] != null && ! values[0].equals("")){
			StringBuffer theQuery = new StringBuffer(query.getQuery());
			if (theQuery.indexOf("WHERE") > 0){
				theQuery.append(" AND ");
			} else {
				theQuery.append(" WHERE ");
			}
			theQuery.append(this.columnToFilter);
			query.setQuery(theQuery.toString());
			
			addConditionAndValue(conditionType, values[0], query);
		}
	}

	/**
	 * @param conditionType
	 * @param value
	 * @return
	 */
	protected void addConditionAndValue(String conditionType, String value, Query q) {
		if(conditionType.equalsIgnoreCase("Equals")){
			q.setQuery(q.getQuery() + "=?");
		} else if (conditionType.equalsIgnoreCase("Contains")){
			q.setQuery(q.getQuery() + " LIKE ?");
			value = "%" + value + "%"; 
		} else if (conditionType.equalsIgnoreCase("Starts With")){
			q.setQuery(q.getQuery() + " LIKE ?");
			value = value + "%";
		} else if (conditionType.equalsIgnoreCase("Ends With")){
			q.setQuery(q.getQuery() + " LIKE ?");
			value = "%" + value;
		} else if (conditionType.equalsIgnoreCase("Not Equal to")){
			q.setQuery(q.getQuery() + " <> ?");
		} else if (conditionType.equalsIgnoreCase("Does not contain")){
			q.setQuery(q.getQuery() + " NOT LIKE ?");
			value = "%" + value + "%";
		}
		q.addValueToQuery(value); 
		
		
	}

	/* (non-Javadoc)
	 * @see com.erclab.internal.xpresso.reports.ReportFilter#toHTML()
	 */
	public String conditionTypesToHTML(Report theReport) {
		StringBuffer sb = new StringBuffer();
		sb.append("<SELECT name=\"Filter");
		sb.append(this.columnToFilter + "Condition");
		sb.append("\"");
		sb.append(" id=\"Filter" + this.columnToFilter + "Condition\" class=\"" +  this.style + "\">");
		
		sb.append("<OPTION value=\"Equals\">Equals</OPTION>");
		sb.append("<OPTION value=\"Contains\">Contains</OPTION>");
		sb.append("<OPTION value=\"Starts With\">Starts With</OPTION>");
		sb.append("<OPTION value=\"Ends With\">Ends With</OPTION>");
		sb.append("<OPTION value=\"Not Equal to\">Not Equal to</OPTION>");
		sb.append("<OPTION value=\"Does not contain\">Does not contain</OPTION>");
		sb.append("</SELECT>");

		return sb.toString();
	}

	/* (non-Javadoc)
	 * @see com.erclab.internal.xpresso.reports.ReportFilter#filterBoxToHTML()
	 */
	public String filterBoxToHTML(Report theReport) {
	
		StringBuffer sb = new StringBuffer();
		sb.append("<INPUT TYPE=\"text\" name=\"Filter");
		sb.append(this.columnToFilter);
		sb.append("\"");
		sb.append(" id=\"Filter" + this.columnToFilter + "\">");
		return sb.toString();
	}

}
